package com.game.doudizhu.service;

import com.game.doudizhu.model.Achievement;
import com.game.doudizhu.repository.AchievementRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
public class AchievementService {

    @Autowired
    private AchievementRepository achievementRepository;

    @Cacheable(value = "achievements", key = "'count:' + #playerId")
    public Integer getPlayerAchievementCount(Long playerId) {
        return achievementRepository.countByPlayerId(playerId);
    }

    public void unlockAchievement(Long playerId, String achievementType) {
        // 检查是否已经解锁
        if (!achievementRepository.existsByPlayerIdAndType(playerId, achievementType)) {
            Achievement achievement = new Achievement();
            achievement.setPlayerId(playerId);
            achievement.setType(achievementType);
            achievement.setUnlockTime(System.currentTimeMillis());
            achievementRepository.save(achievement);
        }
    }

    public boolean hasAchievement(Long playerId, String achievementType) {
        return achievementRepository.existsByPlayerIdAndType(playerId, achievementType);
    }
} 